package com.techfansy.moreImageSelected.ui;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.format.Formatter;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.alltoollib.R;
import com.techfansy.moreImageSelected.ImagePicker;
import com.techfansy.moreImageSelected.bean.ImageItem;
import com.techfansy.moreImageSelected.view.SuperCheckBox;


public class ImagePreviewActivity extends ImagePreviewBaseActivity implements ImagePicker.OnImageSelectedListener, ViewPager.OnPageChangeListener, View.OnClickListener, CompoundButton.OnCheckedChangeListener {

    public static final String ISORIGIN = "isOrigin";

    private boolean isOrigin;                      //是否选中原图
    private SuperCheckBox mCbCheck;                //是否选中当前图片的CheckBox
    private SuperCheckBox mCbOrigin;               //原图
    private Button mBtnOk;                         //确认图片的选择
    private ImageView mBtnBack;                       //返回
    private View bottomBar;
    private TextView mSelectedNum;                 //选中图片个数
    private ImageItem item;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        isOrigin = getIntent().getBooleanExtra(ImagePreviewActivity.ISORIGIN, false);
        imagePicker.addOnImageSelectedListener(this);


        initView();
        //确定原图选中状态
        isOriginSelect();
        //初始化当前页面的状态
        onImageSelected(0, null, false);
        if (mImageItems.size() > mCurrentPosition)
            item = mImageItems.get(mCurrentPosition);
        boolean isSelected = imagePicker.isSelect(item);
        mCbCheck.setChecked(isSelected);
        updateOriginImageSize();

        mViewPager.addOnPageChangeListener(this);
        mCbCheck.setOnClickListener(this);
    }
    //确定原图选中状态
    private void isOriginSelect() {
        if (isOrigin)mCbCheck.setChecked(true);
    }

    private void initView() {
        mBtnOk = (Button) findViewById(R.id.btn_ok);
        mBtnOk.setVisibility(View.VISIBLE);
        mBtnOk.setOnClickListener(this);

        mBtnBack = (ImageView) findViewById(R.id.prieview_btn_back);
        mBtnBack.setOnClickListener(this);

        bottomBar = findViewById(R.id.bottom_bar);
        bottomBar.setVisibility(View.VISIBLE);

        mCbCheck = (SuperCheckBox) topBar.findViewById(R.id.prieview_cb_check);
        mCbOrigin = (SuperCheckBox) findViewById(R.id.cb_origin);

        if (isOrigin == false) {
            mCbOrigin.setText(getString(R.string.origin));
            mCbOrigin.setTextColor(Color.parseColor("#acadac"));
            mCbOrigin.setChecked(false);
        }
        mCbOrigin.setOnCheckedChangeListener(this);
        mCbOrigin.setChecked(isOrigin);

        mSelectedNum = (TextView) findViewById(R.id.selected_num_preview);

    }


    //滑动ViewPager的时候，根据外界的数据改变当前的选中状态和当前的图片的位置描述文本
    @Override
    public void onPageSelected(int position) {
        mCurrentPosition = position;
        ImageItem item = mImageItems.get(mCurrentPosition);
        boolean isSelected = imagePicker.isSelect(item);
        mCbCheck.setChecked(isSelected);
        updateOriginImageSize();
    }


    //更新   选中原图每张图的size
    private void updateOriginImageSize() {
        if (isOrigin == true) {
            ImageItem imageItem = mImageItems.get(mCurrentPosition);
            String fileSize = Formatter.formatFileSize(ImagePreviewActivity.this, imageItem.getSize());
            mCbOrigin.setText(getString(R.string.origin_size, fileSize));
        }

    }

    /**
     * 图片添加成功后，修改当前图片的选中数量
     * 当调用 addSelectedImageItem 或 deleteSelectedImageItem 都会触发当前回调
     */
    @Override
    public void onImageSelected(int position, ImageItem item, boolean isAdd) {
        if (imagePicker.getSelectImageCount() > 0) {
            mSelectedNum.setVisibility(View.VISIBLE);
            mSelectedNum.setText("" + imagePicker.getSelectImageCount());
            mBtnOk.setText("完成");
            mBtnOk.setEnabled(true);
        } else {
            mSelectedNum.setVisibility(View.GONE);
            mBtnOk.setText(getString(R.string.complete));
//            mBtnOk.setEnabled(false);
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btn_ok) { //完成
            Intent intent = new Intent();
            intent.putExtra(ImagePicker.EXTRA_RESULT_ITEMS, imagePicker.getSelectedImages());
            intent.putExtra(ImagePreviewActivity.ISORIGIN, isOrigin);
            setResult(ImagePicker.RESULT_CODE_ITEMS, intent);
            finish();
        } else if (id == R.id.prieview_btn_back) { //返回
            Intent intent = new Intent();
            intent.putExtra(ImagePreviewActivity.ISORIGIN, isOrigin);
            setResult(ImagePicker.RESULT_CODE_BACK, intent);
            finish();
        } else if (id == R.id.prieview_cb_check) {   //每一张的选择
            //当点击当前选中按钮的时候，需要根据当前的选中状态添加和移除图片
            ImageItem imageItem = mImageItems.get(mCurrentPosition);
            int selectLimit = imagePicker.getSelectLimit();
            if (mCbCheck.isChecked() && selectedImages.size() >= selectLimit) {
                Toast.makeText(ImagePreviewActivity.this, ImagePreviewActivity.this.getString(R.string.select_limit, selectLimit), Toast.LENGTH_SHORT).show();
                mCbCheck.setChecked(false);
            } else {
                imagePicker.addSelectedImageItem(mCurrentPosition, imageItem, mCbCheck.isChecked());

                //每次选择一张图片就计算一次图片总大小
                if (selectedImages != null && selectedImages.size() > 0) {
                    updateOriginImageSize();
                } else {
                    mCbOrigin.setText(getString(R.string.origin));
                }
            }
        }
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putExtra(ImagePreviewActivity.ISORIGIN, isOrigin);
        setResult(ImagePicker.RESULT_CODE_BACK, intent);
        finish();
        super.onBackPressed();
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        int id = buttonView.getId();
        if (id == R.id.cb_origin) {
            if (isChecked) {

                //每张图片的size
                ImageItem imageItem = mImageItems.get(mCurrentPosition);
                String fileSize = Formatter.formatFileSize(ImagePreviewActivity.this, imageItem.getSize());
                mCbOrigin.setText(getString(R.string.origin_size, fileSize));

                mCbOrigin.setTextColor(Color.parseColor("#030303"));
                isOrigin = true;

            } else {
                mCbOrigin.setTextColor(Color.parseColor("#acadac"));
                mCbOrigin.setText(getString(R.string.origin));
                isOrigin = false;
            }

            //假如点击原图时当前照片选中个数==0 ，则点击原图按钮时该图片为选中状态
            if (imagePicker.getSelectImageCount() == 0) {
                mCbCheck.setChecked(true);
                ImageItem item = mImageItems.get(mCurrentPosition);
                imagePicker.addSelectedImageItem(mCurrentPosition, item, isChecked);
            }
        }
    }

    @Override
    protected void onDestroy() {
        imagePicker.removeOnImageSelectedListener(this);
        super.onDestroy();
    }

    /**
     * 单击时，隐藏头和尾
     */
    @Override
    public void onImageSingleTap() {
        if (topBar.getVisibility() == View.VISIBLE) {
            topBar.setAnimation(AnimationUtils.loadAnimation(this, R.anim.top_out));
            bottomBar.setAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_out));
            topBar.setVisibility(View.GONE);
            bottomBar.setVisibility(View.GONE);
            tintManager.setStatusBarTintResource(R.color.transparent);//通知栏所需颜色
            //给最外层布局加上这个属性表示，Activity全屏显示，且状态栏被隐藏覆盖掉。
            if (Build.VERSION.SDK_INT >= 16)
                content.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
        } else {
            topBar.setAnimation(AnimationUtils.loadAnimation(this, R.anim.top_in));
            bottomBar.setAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_in));
            topBar.setVisibility(View.VISIBLE);
            bottomBar.setVisibility(View.VISIBLE);
            tintManager.setStatusBarTintResource(R.color.status_bar);//通知栏所需颜色
            //Activity全屏显示，但状态栏不会被隐藏覆盖，状态栏依然可见，Activity顶端布局部分会被状态遮住
            if (Build.VERSION.SDK_INT >= 16)
                content.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
